/**
 * @desc 创建一个查询语句构造器
 * @return {{select: (function(*): select), build: (function(): string), limit: (function(*): limit), orderBy: (function(*): orderBy), from: (function(*): from), where: (function(*): where)}}
 */
function createSqlBuilder() {
    let sql = '';

    function select(columns) {
        sql += 'SELECT ';
        if (columns) {

            sql += columns.join(', ');
        } else {
            sql += '*';
        }
        sql += ' FROM ';
        return this;
    }

    function from(table) {
        sql += table;
        sql += ' WHERE ';
        return this;
    }

    function where(condition) {
        sql += condition;
        sql += ' ORDER BY ';
        return this;
    }

    function orderBy(column) {
        sql += column;
        sql += ' LIMIT ';
        return this;
    }

    function limit(count) {
        sql += count;
        return this;
    }

    function build() {
        return sql;
    }

    return {
        select,
        from,
        where,
        orderBy,
        limit,
        build
    };
}

// 使用示例：
// const sqlBuilder = createSqlBuilder();

/**
 *
 * @type {string|*}
 */
// const query = sqlBuilder.select(['id','name','age'])
//     .from('users')
//     .where('age > 10')
//     .orderBy('name')
//     .limit('20,20').build();
// console.log(query); // 输出：SELECT id FROM users WHERE age > 30 ORDER BY name LIMIT 10

module.exports = createSqlBuilder;
